<?php
    session_start();
    include "../../function/functionPHP.php";
    include "../../class/farm.class.php";
    include "../../class/rice.class.php";
    include "../../class/crop.class.php";
    include "../../class/member.class.php";
    include "../../class/date.class.php";
    $mysqli=  connect();
    
    if(isset($_POST["startYear"])){
        $startYear=$_POST["startYear"];
    }else{
        $startYear="";
    }
                
    $reportMember=$_SESSION["report"]["member"];
    $keyword=$_POST["keyword"];
    $temp="";
    if(is_array($reportMember)){
        foreach ($reportMember as $key => $value) {
            $temp.="'$key',";
        }
    }
    $temp=  substr($temp, 0,  strlen($temp)-1);
?>
<div style="font-family: Supermarket;font-size: 120%;text-align: center;margin-bottom: 10px;margin-top: 10px;">
    ตารางรายชื่อเกษตรกร <?php
        if($startYear==""){
            echo "ทั้งหมด";
        }
    ?><br>
    <span style='font-size: 80%;'>
        <?php
            if($startYear==""){
                if($keyword!=""){
                    echo "(คำค้น : ".$keyword.")";
                }
            }else{
                echo "เฉพาะ Crop ที่เริ่มปลูกภายในปี ".$startYear;
                if($keyword!=""){
                    echo " (คำค้น : ".$keyword.")";
                }
            }
        ?>
    </span>
</div>
<table class="noSpacing"style="width: 100%;height: 100%;">
    <tr>
        <td style="vertical-align: top;padding: 0px;">
            <form id="selectCropForm">
                <input type='hidden' id="costID" name='costID'>
                <style type="text/css">
                    .tableHeader{
                        font-family: Supermarket;
                        font-size: 80%;
                        padding: 10px;
                        background-color: #428bca;
                        color: #fff;
                    }
                    .memberListTable td{
                        padding: 10px;
                    }
                </style>
                <table class="noSpacing memberListTable pc" style="width: 100%;">
                    <tr>
                        <td class="tableHeader">ชื่อ-นามสกุล</td>
                        <td class="tableHeader" style="text-align: right;">ต้นทุน (บาท/ไร่)</td>
                        <td class="tableHeader" style="text-align: right;">กำไร (บาท/ไร่)</td>
                        <td class="tableHeader" style="text-align: right;">กำไร (%)</td>
                    </tr>
                    <?php
                        $queryCoop="
                            select  *
                            from    coop_type
                            where   coopID in (
                                        select  coopID
                                        from    member
                                    )
                            ";
                        $resultCoop=$mysqli->query($queryCoop);
                        while($rowCoop=$resultCoop->fetch_array(MYSQLI_ASSOC)){
                            ?>
                    <tr style="cursor: pointer;">
                        <td style="padding: 30px 10px 10px 10px;width: 10px;border-bottom: 2px solid #298EC0" colspan="4">
                            <strong><?=$rowCoop["coopName"]?></strong>
                        </td>
                    </tr>
                        <?php
                            if($startYear==""){
                                $query="
                                    select  *
                                    from    member_farm_crop
                                    where   farmID in (
                                                select  farmID
                                                from    member_farm
                                                where   memID in (
                                                            select  memID
                                                            from    member
                                                            where   (
                                                                        firstName like '%".$keyword."%' or
                                                                        lastName like '%".$keyword."%'
                                                                    ) and
                                                                    coopID='".$rowCoop["coopID"]."'
                                                        )
                                            )
                                    ";
                            }else{
                                $query="
                                    select  *
                                    from    member_farm_crop
                                    where   farmID in (
                                                select  farmID
                                                from    member_farm
                                                where   memID in (
                                                            select  memID
                                                            from    member
                                                            where   (
                                                                        firstName like '%".$keyword."%' or
                                                                        lastName like '%".$keyword."%'
                                                                    ) and
                                                                    coopID='".$rowCoop["coopID"]."'
                                                        )
                                            ) and
                                            year(startDate)=".$startYear."
                                    ";
                            }
                            $result=$mysqli->query($query);
                            while($row=$result->fetch_array(MYSQLI_ASSOC)){
                                $farmClass=new farm($row["farmID"]);
                                $farm=$farmClass->getFarm();
                                $cropObj=new crop($row["cropID"]);
                                $cost=$cropObj->sumCost();
                                $profit=$cropObj->profit();
                                
                                $crop=$cropObj->getCrop();
                                $riceObj=new rice($crop["rvID"]);
                                $rice=$riceObj->getRice();

                                $memberClass=new member($farm["memID"]);
                                $member=$memberClass->getMember();

                                $dateClass=new date();
                                
                                if($cost>0){
                                    $profitPercent=($profit*100)/$cost;
                                }else{
                                    $profitPercent=0;
                                }
                                
                                $array[]=array(
                                    "cropID"=>$row["cropID"],
                                    "dateRange"=>$dateClass->dateEncodeBE($row["startDate"])." - ".$dateClass->dateEncodeBE($row["endDate"]),
                                    "cost"=>$cost,
                                    "profit"=>$profit,
                                    "profitPercent"=>$profitPercent,
                                    "memberName"=>$member["firstName"]." ".$member["lastName"],
                                    "rvName"=>$rice["rvName"]
                                );

                            }
                            if(is_array($array)){
                                $array= arraySort($array, "profitPercent", SORT_DESC);
                            }
                            
                            //array_multisort(array("profitPercent"),SORT_DESC,$array); 
                            for($i=0;$array[$i]!=null;$i++){
                                ?>
                        <tr style="cursor: pointer;border-bottom: 1px solid #AFDFF5">
                            <td>
                                <?=$array[$i]["memberName"]?> (<?=$array[$i]["rvName"]?>)
                                <div style="font-size: 70%;"><?=$array[$i]["dateRange"]?></div>
                            </td>
                            <td style="padding: 5px;border-bottom: 1px solid #AFDFF5;text-align: right;">
                                <?php
                                    echo number_format($array[$i]["cost"],2);
                                ?>
                            </td>
                            <td style="padding: 5px;border-bottom: 1px solid #AFDFF5;text-align: right;">
                                <?php
                                    echo number_format($array[$i]["profit"],2);
                                ?>
                            </td>
                            <td style="padding: 5px;border-bottom: 1px solid #AFDFF5;text-align: right;">
                                <?php
                                    if($array[$i]["profitPercent"]>0){
                                        echo number_format($array[$i]["profitPercent"],2)."%";
                                    }
                                ?>
                            </td>
                        </tr>
                                    <?php
                            }
                            unset($array);
                        }
                    ?>
                </table>
                <table class="noSpacing memberListTable mobile" style="width: 100%;">
                    <tr>
                        <td class="tableHeader">ชื่อ-นามสกุล</td>
                        <td class="tableHeader" style="text-align: right;">ต้นทุน (บาท/ไร่)</td>
                        <td class="tableHeader" style="text-align: right;">กำไร (บาท/ไร่)</td>
                        <td class="tableHeader" style="text-align: right;">กำไร (%)</td>
                    </tr>
                    <?php
                        $queryCoop="
                            select  *
                            from    coop_type
                            where   coopID in (
                                        select  coopID
                                        from    member
                                    )
                            ";
                        $resultCoop=$mysqli->query($queryCoop);
                        while($rowCoop=$resultCoop->fetch_array(MYSQLI_ASSOC)){
                            ?>
                    <tr style="cursor: pointer;">
                        <td style="padding: 30px 10px 10px 10px;width: 10px;border-bottom: 2px solid #298EC0" colspan="4">
                            <strong><?=$rowCoop["coopName"]?></strong>
                        </td>
                    </tr>
                        <?php
                            if($startYear==""){
                                $query="
                                    select  *
                                    from    member_farm_crop
                                    where   farmID in (
                                                select  farmID
                                                from    member_farm
                                                where   memID in (
                                                            select  memID
                                                            from    member
                                                            where   (
                                                                        firstName like '%".$keyword."%' or
                                                                        lastName like '%".$keyword."%'
                                                                    ) and
                                                                    coopID='".$rowCoop["coopID"]."'
                                                        )
                                            )
                                    ";
                            }else{
                                $query="
                                    select  *
                                    from    member_farm_crop
                                    where   farmID in (
                                                select  farmID
                                                from    member_farm
                                                where   memID in (
                                                            select  memID
                                                            from    member
                                                            where   (
                                                                        firstName like '%".$keyword."%' or
                                                                        lastName like '%".$keyword."%'
                                                                    ) and
                                                                    coopID='".$rowCoop["coopID"]."'
                                                        )
                                            ) and
                                            year(startDate)=".$startYear."
                                    ";
                            }
                            $result=$mysqli->query($query);
                            while($row=$result->fetch_array(MYSQLI_ASSOC)){
                                $farmClass=new farm($row["farmID"]);
                                $farm=$farmClass->getFarm();
                                $cropObj=new crop($row["cropID"]);
                                $crop=$cropObj->getCrop();
                                $riceObj=new rice($crop["rvID"]);
                                $rice=$riceObj->getRice();
                                $cost=$cropObj->sumCost();
                                $profit=$cropObj->profit();

                                $memberClass=new member($farm["memID"]);
                                $member=$memberClass->getMember();

                                $dateClass=new date();
                                
                                if($cost>0){
                                    $profitPercent=($profit*100)/$cost;
                                }else{
                                    $profitPercent=0;
                                }
                                
                                $array[]=array(
                                    "cropID"=>$row["cropID"],
                                    "dateRange"=>$dateClass->dateEncodeBE($row["startDate"])." - ".$dateClass->dateEncodeBE($row["endDate"]),
                                    "cost"=>$cost,
                                    "profit"=>$profit,
                                    "profitPercent"=>$profitPercent,
                                    "memberName"=>$member["firstName"]." ".$member["lastName"],
                                    "rvName"=>$rice["rvName"]
                                );

                            }
                            $array= arraySort($array, "profitPercent", SORT_DESC);
                            
                            //array_multisort(array("profitPercent"),SORT_DESC,$array); 
                            for($i=0;$array[$i]!=null;$i++){
                                ?>
                        <tr style="cursor: pointer;border-bottom: 1px solid #AFDFF5">
                            <td>
                                <?=$array[$i]["memberName"]?> (<?=$array[$i]["rvName"]?>)
                                <div style="font-size: 70%;"><?=$array[$i]["dateRange"]?></div>
                            </td>
                            <td style="padding: 5px;border-bottom: 1px solid #AFDFF5;text-align: right;">
                                <?php
                                    echo number_format($array[$i]["cost"],2);
                                ?>
                            </td>
                            <td style="padding: 5px;border-bottom: 1px solid #AFDFF5;text-align: right;">
                                <?php
                                    echo number_format($array[$i]["profit"],2);
                                ?>
                            </td>
                            <td style="padding: 5px;border-bottom: 1px solid #AFDFF5;text-align: right;">
                                <?php
                                    if($array[$i]["profitPercent"]>0){
                                        echo number_format($array[$i]["profitPercent"],2)."%";
                                    }
                                ?>
                            </td>
                        </tr>
                                    <?php
                            }
                            unset($array);
                        }
                    ?>
                </table>
            </form>
        </td>
    </tr>
</table>